<template>
  <div class="goods-item" @click="goodsClick">
    <img :src="showImg" alt="" @load="imgLoad" />
    <div class="goods-info">
      <p>{{ goodsItem.title }}</p>
      <div class="goods-hot">
        <span class="price">￥{{ goodsItem.price }}</span>
        <span class="fav">{{ goodsItem.cfav }}</span>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'GoodListItem',
  props: {
    goodsItem: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  computed: {
    showImg() {
      return (
        this.goodsItem.img || this.goodsItem.image || this.goodsItem.show.img
      )
    },
    itemId() {
      return this.goodsItem.iid || this.goodsItem.item_id
    }
  },
  methods: {
    imgLoad() {
      // //发送事件至事件总线
      // this.$bus.$emit('imgLoad')

      //这个事件会发送出去,给所有的监听对象,但是在商品也中,这个事件触发,会让首页触发事件,所以不好,需要解决
      //1.判断状态,发送  缺点: 每一个路由都需要进行判断,复用性差
      // if (this.$route.path.indexOf('/home') !== -1) {
      //   //发送事件至事件总线
      //   this.$bus.$emit('imgLoad')
      // } else if (this.$route.path.indexOf('/detaile') !== -1) {
      //   //发送事件至事件总线
      //   this.$bus.$emit('imgLoad')
      // }
      //2.照常发送,在页面中进行判断,退出页面后关闭对应的监听事件
      this.$bus.$emit('imgLoad')
    },
    goodsClick() {
      this.$router.push('/detail/' + this.itemId)
    }
  }
}
</script>

<style type="text/css" scoped>
.goods-item {
  width: 48%;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  margin-bottom: 20px;
}
.goods-hot {
  display: flex;
  margin-top: 5px;
  padding: 0 5px;
  justify-content: space-between;
}
.price {
  font-weight: bold;
  color: var(--color-tint);
}
.fav {
  font-size: 14px;
  line-height: 18px;
  padding-left: 20px;
  background: url('~assets/images/common/collect.svg') no-repeat 2px 0;
  background-size: 16px 16px;
}
.goods-info p {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
img {
  width: 100%;
  border-radius: 8px;
  margin-bottom: 8px;
}
</style>
